﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SiemensOPC;
using Opc.Ua;
using Opc.Ua.Client;
using VswService_V7.Config;
using VswService_V7.DataBase.Tables;
using VswService_V7.Utility;

namespace VswService_V7.OPC.DB
{

    //FltAddressDbData
    public class FltAddressDbData : SubscriptionDbDataBase
    {
        public FltAddressDbData(SimaticOPCClient opc, NodeConfig nodes) : base(opc, nodes)
        {
            samplePipe = new SampleInfo(_Location);

        }

        SampleInfo samplePipe;
        string _Location = "FLT";

        public override void CreateSubscription()
        {
            opcServer.QuickAddMonitorItem(opcNodes.FltAddress.GrpoupName,
                opcNodes.FltAddress.PublishTime,
                opcNodes.FltAddress.SampleTime,
                opcNodes.FltAddress.GetNodeAddress(),
                opcNodes.FltAddress.GetNodeDisplayName(),
                SubscriptionHandle);
            opcServer.QuickAddMonitorItem(opcNodes.FltResultAddress.GrpoupName,
                opcNodes.FltResultAddress.PublishTime,
                opcNodes.FltResultAddress.SampleTime,
                opcNodes.FltResultAddress.GetNodeAddress(),
                opcNodes.FltResultAddress.GetNodeDisplayName(),
                SubscriptionHandlePipe);
        }

        private void SubscriptionHandle(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
            if (notification == null || notification.Value.Value == null)
                return;
            if (monitoredItem.DisplayName == "FltAsk")
            {
                bool ask;
                if (bool.TryParse(notification.Value.Value.ToString(), out ask))
                {
                    if (ask)
                    {
                        Console.WriteLine(_Location + " ask: " + notification.Value);
                        Task.Run(() => HandleFLT());
                    }
                    else
                    {
                        opcServer.WriteValue("false", opcNodes.FltAddress.FltRsp);
                    }
                }
            }

        }

        private void SubscriptionHandlePipe(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                MonitoredItemNotification notification = e.NotificationValue as MonitoredItemNotification;
                if (notification == null)
                    return;
                switch (monitoredItem.DisplayName)
                {
                    case "Batch_No":
                        {
                            samplePipe.batchNo = notification.Value.Value.ToString();
                            break;
                        }
                    case "Pipe_No":
                        {
                            samplePipe.pipeNo = notification.Value.Value.ToString();
                            break;
                        }
                    case "Sample_Type":
                        {
                            samplePipe.sampleType = notification.Value.Value.ToString();
                            break;
                        }
                    case "Sample_Seq":
                        {
                            samplePipe.sampleSeq = notification.Value.Value.ToString();
                            break;
                        }
                    case "Sample_Result":
                        {
                            samplePipe.sampleResult = notification.Value.Value.ToString();
                            break;
                        }
                    case "Flt_Gap":
                        {
                            samplePipe.fltGap = Formats.ConvertUnit(notification.Value.Value.ToString(),VswUnit.FLT_Gap_L1,1);
                            break;
                        }
                    case "Flt_Time":
                        {
                            samplePipe.fltTime = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.FLT_Gap_L1,1);
                            break;
                        }
                    case "Flt_Spd":
                        {
                            samplePipe.fltSpeed = Formats.ConvertUnit(notification.Value.Value.ToString(), VswUnit.FLT_Gap_L1,1);
                            break;
                        }
                    //case "Sample_Result":
                    //    {
                    //        samplePipe.sampleResult = notification.Value.Value.ToString();
                    //        break;
                    //    }
                    default:
                        {
                            break;
                        }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        private async Task HandleFLT()
        {
            samplePipe.updateTime = DateTime.Now.ToString(Formats.GetLongFormat());
            await Task.Delay(2000);
            if (samplePipe.batchNo.Equals("0") || samplePipe.pipeNo.Equals("0"))
                return;
            //QC CODE 高低位交换
            Console.WriteLine(_Location + " handleqc start record");
            try
            {
                SAMPLE_TABLE.UpdateSampleResult(samplePipe);
            }
            catch(Exception ex)
            {
                LogHelper.WriteLog("HandleFLT", ex);
            }
            
            opcServer.WriteValue("true", opcNodes.FltAddress.FltRsp);
        }
    }
}
